<?php
/**
 * @Name: Toole.php
 * @Author: hug-code
 */

namespace HugCode\WeChat\Basics;


class Toole
{

    /**
     * @Desc 生成随机字符串
     * @param int $length
     * @return string
     * @author hug-code
     * @Time 2021/7/9 11:44
     */
    public static function generateRandomStr(int $length = 16)
    {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str   = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= $chars[mt_rand(0, strlen($chars) - 1)];
        }
        return $str;
    }

    /**
     * @Desc 数组转json
     * @param array $data
     * @return false|string
     * @author yashuai 1762910894@qq.com
     */
    public static function arr2json(array $data)
    {
        $json = json_encode($data, JSON_UNESCAPED_UNICODE);
        return $json === '[]' ? '{}' : $json;
    }

}
